package com.liaowei.auth.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liaowei.auth.mapper.PermissionMapper;
import com.liaowei.auth.mapper.RoleMapper;
import com.liaowei.auth.mapper.UserMapper;
import com.liaowei.auth.model.vo.UserVO;
import com.liaowei.auth.service.UserService;
import com.liaowei.common.ErrorCode;
import com.liaowei.exception.CustomerException;
import com.liaowei.model.domain.User;
import com.liaowei.model.param.LoginUserParam;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author LindaMan
 * @description 针对表【wy_user(用户表)】的数据库操作Service实现
 * @createDate 2023-05-03 13:27:55
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    private final UserMapper userMapper;
    private final RoleMapper roleMapper;
    private final PermissionMapper permissionMapper;


    /**
     * 查询用户权限
     * @return 权限列表
     */
    @Override
    public List<String> roleNameList(Long userId) {
        return Optional.ofNullable(roleMapper.selectRoleNameListByUserId(userId)).orElse(new ArrayList<>());
    }

    /**
     * 查询用户角色
     * @return 角色列表
     */
    @Override
    public List<String> permissionNameList(Long userId) {
        return Optional.ofNullable(permissionMapper.selectPermissionNameListByUserId(userId)).orElse(new ArrayList<>());
    }

    /**
     * 用户登录认证
     *
     * @param loginUserParam 登录参数
     * @return 完整用户信息
     */
    @Override
    public UserVO login(LoginUserParam loginUserParam) {
        // 根据用户名从数据库中查询对应用户
        LambdaQueryWrapper<User> authWrapper = Wrappers.lambdaQuery(User.class).eq(StrUtil.isNotBlank(loginUserParam.getUsername()),
                User::getUsername, loginUserParam.getUsername());
        User user = userMapper.selectOne(authWrapper);
        if (BeanUtil.isEmpty(user)) { // 用户为空
            // 抛出用户名或密码错误
            throw new CustomerException(ErrorCode.USERNAME_PASSWORD_ERROR);
        }
        // 用户不为空
        String inputPassword = loginUserParam.getPassword(); // 用户输入的密码
        String dbPassword = user.getPassword(); // 数据库中的密码
        // 校验密码
        if (!BCrypt.checkpw(inputPassword, dbPassword)) { // 校验失败
            // 抛出用户名或密码错误
            throw new CustomerException(ErrorCode.USERNAME_PASSWORD_ERROR);
        }
        // 保存用户登录信息 并存储到redis
        StpUtil.login(user.getId());

        // 封装userVO
        UserVO userVO = new UserVO();
        BeanUtil.copyProperties(user, userVO);
        userVO.setToken(StpUtil.getTokenInfo());

        // 脱敏
        userVO.setPassword(null);

        // 返回结果
        return userVO;
    }
}




